#!/bin/bash
# 显示所有服务状态 
# author    : guyadong
# date      : 2019/09/07

sh_folder=$(dirname $(readlink -f $0))
cd $sh_folder

# 脚本初始化
. ./common_init.sh

# 显示服务状态
function show_service_status(){

    while [ $# -ge 1 ]
    do
        local color=31
        local act=$(systemctl is-active $1)
        [ "$act" = "active" ] >/dev/null && color=32
        local mpid="$(systemctl show $1 --property=MainPID)"
        mpid=${mpid##*=}
        local ports=()
        if [ $mpid -ne 0 ]
        then
            local nids=($(ps -o pid -g $mpid|sed -e '1d'))
            for p in ${nids[@]}
            do
                ports=(${ports[@]} $(netstat -nlp 2>/dev/null|grep $p |grep -E ':[[:digit:]]+' -o | sed 's/://g'))
            done
        fi
        ports=${ports[@]}
        echo -e "service $1 \033[${color}m$act \033[0m MainPID:$mpid PORT:${ports// /,}"
        shift 1
    done
}

show_service_status facelog fdevmgr webredis nginx mysql redis-server-$redis_port erpc_proxy
